﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Draco.Commons.Container
{
    /// <summary>
    /// 自动排序的数组列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class OrderList<T>
    {
        private List<OrderItem> m_InnerList = new List<OrderItem>();
	
	    public OrderList(){
    		
	    }
        /// <summary>
        /// 获取集合包含的元素数量
        /// </summary>
        public int Count
        {
            get { return m_InnerList.Count; }
        }
	
	    /// <summary>
	    /// 添加元素
	    /// </summary>
	    /// <param name="obj"></param>
	    /// <param name="order"></param>
	    public void Add(T obj,int order){
            OrderItem Item = new OrderItem(obj, order * 10000 + Count);
		    m_InnerList.Add(Item);
            m_InnerList.Sort();
	    }
	    /// <summary>
        /// 获取数组
	    /// </summary>
	    /// <returns></returns>
	    public List<T> GetList(){
		    List<T> list = new List<T>();
		    foreach(OrderItem item in m_InnerList){
			    list.Add(item.item);
		    }
		    return list;
	    }
	
	    /// <summary>
	    /// 内部可比较元素
	    /// </summary>
	    class OrderItem : IComparable
        {
		    public T item;
            public long order;
		    public OrderItem(T _item,long _order)
            {
			    item = _item;
			    order = _order;
		    }
            public int CompareTo(object obj)
            {
                return (int)(order - ((OrderItem)obj).order);
            }
        }
    }
}
